package com.lyf.scm.admin.domain.remote.allocationservice.facade;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.lyf.scm.admin.api.dto.EsAggregationDTO;
import com.lyf.scm.admin.api.dto.EsExportAggregationDTO;
import com.lyf.scm.admin.api.dto.EsReturnDTO;
import com.lyf.scm.admin.domain.remote.allocationservice.SalesPlanEsLazyLoadingRemote;
import com.lyf.scm.common.enums.ResponseMsg;
import com.rome.arch.core.clientobject.Response;
import com.rome.arch.core.exception.RomeException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * es懒加载查询解决方案
 *
 * @author wangchlei
 * @date 2020/11/14
 */
@Service
@Slf4j
public class SalesPlanEsLazyLoadingFacade {
    @Resource
    private SalesPlanEsLazyLoadingRemote salesPlanEsLazyLoadingRemote;

    public PageInfo<EsReturnDTO> pageQuery(EsAggregationDTO esAggregation) {
        log.info("入参：{}", JSONObject.toJSONString(esAggregation));
        Response<PageInfo<EsReturnDTO>> response = salesPlanEsLazyLoadingRemote.pageQuery(esAggregation);
        log.info("返回结果：{}", JSONObject.toJSONString(response));
        if (!response.getCode().equals(ResponseMsg.SUCCESS.getCode())) {
            throw new RomeException(response.getCode(), response.getMsg());
        }
        return response.getData();
    }

    public List<EsReturnDTO> queryFromCache(EsAggregationDTO esAggregationDTO) {
        log.info("入参：{}", JSONObject.toJSONString(esAggregationDTO));
        Response<List<EsReturnDTO>> response = salesPlanEsLazyLoadingRemote.queryFromCache(esAggregationDTO);
        log.info("返回结果：{}", JSONObject.toJSONString(response));
        if (!response.getCode().equals(ResponseMsg.SUCCESS.getCode())) {
            throw new RomeException(response.getCode(), response.getMsg());
        }
        return response.getData();
    }
}
